﻿//-----------------------------------------------------------------------
// <copyright file="Favourites.cs" company="Microsoft Corporation">
//     Copyright Microsoft Corporation. All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) This is sample code only, do not use in production environments.
// </copyright>
//-----------------------------------------------------------------------
namespace Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.Models
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using Windows.Storage;

    /// <summary>
    /// Defines the type of favourite
    /// </summary>
    internal enum FavouriteType
    {
        /// <summary>
        /// Specifies a category favourite.
        /// </summary>
        Category,

        /// <summary>
        /// Specifies a project favourite.
        /// </summary>
        Project
    }

    /// <summary>
    /// The favourite data management class
    /// </summary>
    internal static class Favourites
    {
        /// <summary>
        /// Toggles the status of a favourite.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="type">The type of a favourite.</param>
        public static void ToggleFavourite(int id, FavouriteType type)
        {
            var container = ApplicationData.Current.RoamingSettings.CreateContainer("favourites", ApplicationDataCreateDisposition.Always);
            var key = string.Format(CultureInfo.InvariantCulture, "{0}-{1}", type, id);
            if (!container.Values.ContainsKey(key))
            {
                container.Values.Add(string.Format(CultureInfo.InvariantCulture, "{0}-{1}", type, id), string.Empty);
            }
            else
            {
                container.Values.Remove(key);
            }
        }

        /// <summary>
        /// Determines whether the specified ID is a favourite.
        /// </summary>
        /// <param name="id">The ID.</param>
        /// <param name="type">The type of favourite.</param>
        /// <returns><c>true</c> if the specified id is favourite; otherwise, <c>false</c>.</returns>
        public static bool IsFavourite(int id, FavouriteType type)
        {
            var container = ApplicationData.Current.RoamingSettings.CreateContainer("favourites", ApplicationDataCreateDisposition.Always);
            var key = string.Format(CultureInfo.InvariantCulture, "{0}-{1}", type, id);
            return container.Values.ContainsKey(key);
        }

        /// <summary>
        /// Adds the favourite.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="type">The type.</param>
        public static void AddFavourite(int id, FavouriteType type)
        {
            var container = ApplicationData.Current.RoamingSettings.CreateContainer("favourites", ApplicationDataCreateDisposition.Always);
            var key = string.Format(CultureInfo.InvariantCulture, "{0}-{1}", type, id);
            if (!container.Values.ContainsKey(key))
            {
                container.Values.Add(string.Format(CultureInfo.InvariantCulture, "{0}-{1}", type, id), string.Empty);
            }
        }

        /// <summary>
        /// Removes the favourite.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="type">The type.</param>
        public static void RemoveFavourite(int id, FavouriteType type)
        {
            var container = ApplicationData.Current.RoamingSettings.CreateContainer("favourites", ApplicationDataCreateDisposition.Always);
            var key = string.Format(CultureInfo.InvariantCulture, "{0}-{1}", type, id);
            if (container.Values.ContainsKey(key))
            {
                container.Values.Add(string.Format(CultureInfo.InvariantCulture, "{0}-{1}", type, id), string.Empty);
            }
        }

        /// <summary>
        /// Gets the favourites.
        /// </summary>
        /// <returns>A collection of favourites.</returns>
        public static IEnumerable<Tuple<FavouriteType, int>> GetFavourites()
        {
            var container = ApplicationData.Current.RoamingSettings.CreateContainer("favourites", ApplicationDataCreateDisposition.Always);
            foreach (var item in container.Values)
            {
                var tokenIndex = item.Key.IndexOf('-');
                var type = (FavouriteType)Enum.Parse(typeof(FavouriteType), item.Key.Substring(0, tokenIndex));
                var id = Convert.ToInt32(item.Key.Substring(tokenIndex + 1));
                yield return Tuple.Create(type, id);
            }
        }
    }
}